<template>
  <div>
    <HomeHeader></HomeHeader>
    <TypeNav></TypeNav>
    <el-form>
      <div class="tender-box">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">盛世绿能招投标系统</el-breadcrumb-item>
          <el-breadcrumb-item >招标信息</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="tender-grid-content">
          <h1 style="text-align: center;padding-top: 20px;">招标信息</h1>
          <div style="margin-top: 20px">
            <el-row :gutter="15">
              <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="70px">
                <el-col :span="23">
                  <el-row type="flex" justify="start" align="middle" :gutter="30">
                    <el-col :span="24">
                      <el-form-item label="时间" prop="field101">
                        <el-date-picker type="daterange" v-model="formData.field101" format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd" :style="{width: '100%'}" start-placeholder="年/月/日"
                                        end-placeholder="年/月/日" range-separator="至" clearable
                        ></el-date-picker>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="项目名称" prop="field108">
                        <el-input v-model="formData.field108" placeholder="请输入项目名称" clearable
                                  :style="{width: '100%'}"
                        ></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="项目状态">
                        <el-select v-model="formData.sProjectState" placeholder="请选择项目状态" @change="change" style="width: 150px">
                          <el-option value="1" label="立项中">立项中</el-option>
                          <el-option value="2" label="招标中">招标中</el-option>
                          <el-option value="3" label="投标中">投标中</el-option>
                          <el-option value="4" label="竞标中">竞标中</el-option>
                          <el-option value="5" label="评标中">评标中</el-option>
                          <el-option value="6" label="定标中">定标中</el-option>
                          <el-option value="7" label="已定标">已定标</el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item size="medium">
                        <el-button type="primary" @click="query">查询</el-button>
                        <el-button @click="resetForm">重置</el-button>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-col>

              </el-form>
            </el-row>
          </div>

          <el-table v-loading="loading" :data="tenderList" @selection-change="handleSelectionChange">
            <el-table-column label="序号" align="center" prop="orderNum" width="70"/>
            <el-table-column label="项目编号" align="center" prop="sCode" width="200"/>
            <el-table-column label="项目名称" align="center" prop="sName"/>
            <el-table-column prop="sWay" label="招标方式" align="center" width="90">
              <template slot-scope="scope">
                <span type="text" v-if="scope.row.sWay === 1">公开招标</span>
                <span v-else>邀请招标</span>
              </template>
            </el-table-column>
            <el-table-column label="项目负责人" align="center" prop="sLeader" width="200">
              <template slot-scope="scope">
                {{ scope.row.sLeader || '——' }}
              </template>
            </el-table-column>
            <el-table-column label="项目状态" align="center" prop="sProjectState" width="90">
              <template slot-scope="scope">
                <span v-if="scope.row.sProjectState === 1">立项中</span>
                <span v-else-if="scope.row.sProjectState === 2">招标中</span>
                <span v-else-if="scope.row.sProjectState === 3">投标中</span>
                <span v-else-if="scope.row.sProjectState === 4">竞标中</span>
                <span v-else-if="scope.row.sProjectState === 5">评标中</span>
                <span v-else-if="scope.row.sProjectState === 6">定标中</span>
                <span v-else>已定标</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding" width="290">
              <template slot-scope="scope">
<!--                <router-link :to="'/tender/bidding?type=bidding&sid='+scope.row.sid">
                  <el-button v-if="scope.row.sProjectState !== 1" size="small" type="primary" icon="el-icon-delete">进入项目</el-button>
                </router-link>-->
                <el-tag class="tender-el-tag" v-if="calculateRemainingDays(scope.row.sDeadline,scope.row.sEndTime) === 1 && (scope.row.sProjectState === 2 || scope.row.sProjectState === 3)">
                  <el-button size="small" type="primary" style="background-color: yellowgreen;border-color: yellowgreen;" @click="biddingTender(scope.row)">
                    <el-link type="primary" style="color: white">我要投标</el-link>
                  </el-button>
                </el-tag>
                <el-tag class="tender-el-tag" v-if="calculateRemainingDays(scope.row.sDeadline,scope.row.sEndTime) === 0 && (scope.row.sProjectState === 2 || scope.row.sProjectState === 3)">
                  <el-button size="small" type="primary" style="background-color: #9d9d9d;border-color: #9d9d9d;" :disabled="true">
                    <el-link type="text" style="color: white" :disabled="true">已过时间</el-link>
                  </el-button>
                </el-tag>
<!--                <router-link :to="'/tender/details?type=details&sid='+scope.row.sid">
                  <el-button style="margin-left: 20px" size="small" type="primary" icon="el-icon-view" @click="">查看</el-button>
                </router-link>-->
                <el-button size="small" type="primary" style="margin-left: 20px" @click="viewTender(scope.row)">
                  <el-link type="primary" style="color: white">招标详情</el-link>
                </el-button>
              </template>
            </el-table-column>
            <el-table-column v-for="column in tableColumns" :key="column.prop" :label="column.label" :align="column.align" :prop="column.prop" :width="column.width"/>
          </el-table>
          <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
          />
        </div>
      </div>
    </el-form>
    <HomeFloor></HomeFloor>
  </div>
</template>
<script>
import HomeFloor from "@/components/home/HomeFloor.vue";
import HomeHeader from "@/components/home/HomeHeader";
import TypeNav from "@/components/home/TypeNav";
import Pagination from "@/components/Pagination/index.vue";
import { listTender, delTender} from '@/api/system/tender.js'
import modal from "@/plugins/modal";


export default {
  name: 'tenderList',
  components:{
    HomeHeader,
    TypeNav,
    HomeFloor,
    Pagination
  },
  data() {
    return {
      tableColumns: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招标项目表格数据
      tenderList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sCode: null,
        sName: null,
        sWay: null,
        sLeader: null,
        sProjectState: null,
        sDeadline: null,
        sEndTime: null,
        sid:null,
        orderNum:0,
        createTime:null,
        rangeStartTime:null,
        rangeEndTime:null
      },
      // 表单参数
      formData: {
        field101: null,
        field108: '',
        sProjectState: null
      },
      // 表单校验
      rules: {
        field101: [],
        field108: []
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    query() {
      this.queryParams.sName = this.formData.field108;//项目名称
      this.queryParams.sProjectState = this.formData.sProjectState;
      let range = this.formData.field101;//查询时间范围
      console.log(range,"range");
      if(range){
        this.queryParams.rangeStartTime= range[0];//时间范围开始
        this.queryParams.rangeEndTime = range[1];//时间范围截至
      }
      this.queryParams.rangeStartTime= null;//时间范围开始
      this.queryParams.rangeEndTime = null;//时间范围截至
      this.handleQuery();//搜索按钮点击方法
    },
    /* 下拉框选中 */
    change(value) {
      if (value !== 0)
        this.queryParams.sProjectState = value;
      else
        this.queryParams.sProjectState = null
      this.getList();
    },
    /**
     * 重置
     */
    resetForm() {
      this.$refs.elForm.resetFields();
      this.formData.sProjectState = null;
      this.query();
    },
    /** 查询招标项目列表 */
    getList() {
      this.loading = true;
      this.tenderList=[];
      listTender(this.queryParams).then(response => {
        response.data.rows.forEach((e,i)=>{
          e.orderNum = i+1;
          this.tenderList.push(e);
        })
        this.total = response.data.total;
        this.loading = false;
      });
    },
    viewTender(row) {
      if (sessionStorage.getItem("token") == null) {
        modal.alertWarning("注册供应商并登录后查看")
      } else {
        this.$router.push('/tender/details?type=details&sid='+row.sid);
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderNum: 0,
        sCode: null,
        sName: null,
        sWay: null,
        sLeader: null,
        sProjectState: null,
        sDeadline: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const sid = row.sid || this.ids
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sids = row.sid || this.ids;
      this.$modal.confirm('是否确认删除招标项目编号为"' + sids + '"的数据项？').then(function() {
        return delTender(sids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    biddingTender(row) {
      if (sessionStorage.getItem("token") == null) {
        modal.alertWarning("注册供应商登录后投标")
      } else {
        this.$router.push('/detail?uid='+row.uid+'&type=bidNotice&read=0');
      }
    },
    calculateRemainingDays(sDeadline,sEndTime) {
      // 将字符串转换为日期对象
      const sDeadlineTime = new Date(sDeadline);//报价截止时间
      const sEndTimeTime = new Date(sEndTime);//公示截止时间
      if(sDeadlineTime >= Date.now() && sDeadlineTime >= sEndTimeTime){
        return 1;
      }
      return 0;
    },
  }
}

</script>
<style>
.tender-box {
  width: 1400px;
  min-height: 800px;
  margin: 10px auto;
}

.tender-grid-content {
  margin-top: 21px;
  width: 1400px;
  min-height: 800px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
}

.tender-el-tag {
  background-color: #ecf5ff;
  border-color: #d9ecff;
  height: 34px;
  padding: 0 0px;
  line-height: 30px;
  font-size: 12px;
  color: #0065B5;
  border-width: 1px;
  border-style: solid;
  border-radius: 4px;
  box-sizing: border-box;
  white-space: nowrap;
}

p {
  margin: 10px 0;
}

.demo-table-expand label {
  width: 120px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 80%;
}
/*按钮颜色*/
.el-button--primary {
  color: #FFF;
  background-color: #0065B5;
  border-color: #0065B5;
}
</style>
